package com.qinyi.framework.shiro.service;

import com.qinyi.common.ajax.AjaxCode;
import com.qinyi.common.enums.UserStatus;
import com.qinyi.common.exception.CustomException;
import com.qinyi.system.domin.SysUser;
import com.qinyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author wangan on 2019/6/4
 * @description 用户登录
 * 1.session中验证码是否为空
 * 2.验证码校验
 * 3.用户名与密码空校验
 * 3.用户相关信息交易
 * 4.用户密码校验
 * 5.记录用户登录信息
 */
@Service
public class SysLoginService {

	@Autowired
	private ISysUserService userService;
	@Autowired
	private SysPasswordService passwordService;

	public SysUser login(String username, String password) {
		if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			throw new CustomException(AjaxCode.SERVER_ERROR, "用户名或者密码不能为空");
		}
		SysUser user = userService.selectUserByLoginName(username);
		if (user == null) {
			throw new CustomException(AjaxCode.SERVER_ERROR, "未查询到相关用户");
		}
		if (UserStatus.DISABLE.getStatus().equals(user.getDelFlag())) {
			throw new CustomException(AjaxCode.SERVER_ERROR, "用户已失效");
		}
		if (UserStatus.DELETE.getStatus().equals(user.getDelFlag())) {
			throw new CustomException(AjaxCode.SERVER_ERROR, "用户已删除");
		}
		passwordService.volidate(user, password);
		userService.updateUserInfo(user);
		return user;
	}
}
